//
//  PortfilioListViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/15.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "SamplePhotoCell.h"
#import "CommonOpusModel.h"

#import "PortfilioDetailViewController.h"
#import "PortfilioListViewController.h"

@interface PortfilioListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) NSMutableArray <CommonOpusModel *>*dataArray;

@end

@implementation PortfilioListViewController


- (instancetype)initWithType:(PortfilioSubType)type storeId:(NSString *)storeId classId:(NSString *)classId AndSocId:(NSInteger)socId {

    self = [super init];
    
    if (self) {
        self.type = type;
        self.storeId = storeId;
        self.socId = socId;
        self.classId = classId;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
    [self request_PortList];
}

- (void)initView {
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
    [self.tableview reloadData];
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ceil(190 * ADAPTER_WIDTH);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SamplePhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SamplePhotoCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PortfilioDetailViewController *vc = [[PortfilioDetailViewController alloc] init];
    vc.soId = self.dataArray[indexPath.row].soId;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = UIColor.whiteColor;
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"SamplePhotoCell" bundle:nil] forCellReuseIdentifier:@"SamplePhotoCellID"];
    }
    return _tableview;
}

- (NSMutableArray<CommonOpusModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)request_PortList {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    NSMutableDictionary *t = @{}.mutableCopy;

    [dic setValue:@(1) forKey:@"pageNum"];
    [dic setValue:@(20) forKey:@"pageSize"];
    
    [t setValue:self.storeId forKey:@"sellerId"];
    [t setValue:@(self.socId) forKey:@"socId"];

    if ([self.classId isEqualToString:@"7"]) {
        switch (self.type) {
            case PortfilioSubType_kezhao:{
                [t setValue:@(2) forKey:@"opusType"];

            } break;
            case PortfilioSubType_yangzhao:{
                [t setValue:@(1) forKey:@"opusType"];
            } break;
                
            default:
                break;
        }
    }

    [dic setValue:t forKey:@"t"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_StoreCase];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sellerOpusList = [resultDic valueForKey:@"sellerOpusList"];

                self.dataArray = @[].mutableCopy;
                if ([sellerOpusList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusList.count; i++) {
                        NSDictionary *sellerOpusListDic = sellerOpusList[i];
                        CommonOpusModel *model = [CommonOpusModel modelWithDictionary:sellerOpusListDic];
                        [self.dataArray addObject:model];
                    }
                }
                

                [self.tableview reloadData];
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

@end
